import { getUrlParam } from '@/static/js/filter.js'
export default {
	data() {
		return {
			passShow:true,
			typeShow: 1,
			phone:'',
			password:'',
			showModal:false,
			showContent:'',
			showCancelButton:true, //模态框取消按钮
			confirmText: '确定', //模态框确认文字
			cancelText: '取消', //模态框取消文字
			codeMsg:'获取验证码',
			isSend:false,
			code:'',
			StatusCode:''
		}
	},
	onShow(){
		const code = this.getUrlParam('code')
		if(code){
			if(this.StatusCode != '200'){
				if(!localStorage.openid){
					console.log(code,this.StatusCode,localStorage.openid)
					this.getOpenId(code)
				}
			}
		    
		}
	},
	methods: {
		getUrlParam,
		getCode(){
		    const appId = 'wxc617f43e9fb3b015'
		    const code = this.getUrlParam('code')
		    if(this.StatusCode == '20163'){
		        const local = window.location.origin + window.location.pathname
		        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appId+'&redirect_uri='+encodeURIComponent(local)+'&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect'
		    }else{
		        const local = window.location.href
		        if(code == null || code == ''){
		            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appId+'&redirect_uri='+encodeURIComponent(local)+'&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect'
		        }else{
		            localStorage.setItem('code',code)
		            // this.getOpenId(code)
		        }
		    }
		},
		getOpenId(code){
		    if(!code){
		        this.getCode();
		        return;
		    }
			if(this.StatusCode == '20163'){
			    this.getCode()
			    return;
			}
		    const _this = this;
		    _this.$api.getOpenId(code).then((res)=>{
		        if(res.data.returnCode == '200'){
		            const data = JSON.parse(res.data.message)
		            this.openid = data.openid
					this.StatusCode = '200'
		            localStorage.setItem('openid',data.openid)
		            _this.$api.authorizeLogin({openId:data.openid,access_token:data.access_token,code:code}).then((response)=>{
		                if(response.data.returnCode == 200){
		                    localStorage.setItem('token',response.data.result.token)
		                    uni.redirectTo({
		                    	url: '/pages/index/index'
		                    });
		                }else{
							uni.navigateTo({
								url: '/pages/login/bind?info='+JSON.stringify(response.data.result)
							});
							return;
						}
		            })
		        }else if(res.data.returnCode == '20163'){
		            return;
		        }else{
		            this.getCode()
		        }
		    })
		},
		// 账号密码登录
		login(){
			const _this = this;
			if(!_this.phone){
				_this.showModal = true
				_this.showContent = '请输入手机号'
				_this.showCancelButton = false
			}
			if(_this.typeShow == 1){
				if(!_this.password){
					_this.showModal = true
					_this.showContent = '请输入密码'
					_this.showCancelButton = false
				}
				_this.$api.login({phone:_this.phone,password:_this.password}).then((res)=>{
					if(res.data.returnCode == 200){
						localStorage.setItem('token',res.data.result.token)
						uni.showToast({ title: '登录成功', icon: 'none' });
						uni.hideLoading()
						if (_this.remember) {  //勾选记住
							uni.setStorageSync('account', _this.account);
							uni.setStorageSync('password', _this.password);	
						} else {  //未勾选记住
							uni.removeStorageSync('account');
							uni.removeStorageSync('password');
							_this.account = "";
							_this.password = "";
						}
						uni.redirectTo({
							url: '/pages/index/index'
						});
						// uni.navigateTo({
						// 	url:'/pages/index/index'
						// })
					}else{
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})			
			}else{
				if(!this.code){
					this.showModal = true
					this.showContent = '请输入验证码'
					this.showCancelButton = false
				}
				this.$api.codeLogin({phone:this.phone,code:this.code}).then((res)=>{
					if(res.data.returnCode == '200'){
						localStorage.setItem('token',res.data.result.token)
						uni.navigateTo({
							url:'/pages/index/index'
						})
					}
				})
			}
			
		},
		// 验证手机号
		verifyMobile(){
			if(this.phone){
				this.$api.verifyMobile(this.phone).then((res)=>{
					if(res.data.returnCode == 20011){
						this.showModal = true
						this.showCancelButton = true
						this.showContent = '您的手机还未注册，是否直接注册？'
						this.confirmText = '直接注册'
					}
				})
			}
		},
		// 模态框confim事件
		Modalconfirm(){
			if(this.showCancelButton){
				uni.navigateTo({
					url:'/pages/login/registe'
				})
			}
		},
		// 发送验证码
		sendCode() {
			let _this = this
			if(_this.isSend){
				return;
			}
			//手机正则表达式
			let phoneCode = /^1[3456789]\d{9}$/
			if(_this.phone == '' || !(phoneCode.test(_this.phone))){
				_this.showModal = true
				_this.showCancelButton = false
				_this.showContent = '请输入正确的手机号码'
				return;
			}
			_this.$api.sedNote(_this.phone,'SignIn').then((res)=>{
				if (!_this.timer) {
					_this.count = 60;
					_this.isSend = true;
					_this.timer = setInterval(() => {
						if (_this.count > 0 && _this.count <= 60) {
							_this.codeMsg = _this.count+'s'
							_this.count--;
						} else {
							_this.codeMsg = '获取验证码'
							clearInterval(_this.timer);
							_this.isSend = false;
							_this.timer = null;
						}
					}, 1000)
				}
			})
		},
	}
}